using System;
using System.Collections.Generic;
using System.Text;
using VSLangProj;
using System.Windows.Forms;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.Package.Automation;
using NBusiness.Data;
using NBusiness.Utilities;
using System.Data.Common;
using NBusiness.CodeDom;
using NBusiness.Templates;
using System.IO.IsolatedStorage;
using System.IO;
using System.Reflection;

namespace NBusiness.VisualStudio
{
    internal class ESharpSchemaUpdater
    {
        private ESharpLanguageService _languageService;
        private ESharpProjectNode _projectNode;
        private References _references;

        public ESharpSchemaUpdater(ESharpLanguageService languageService, ESharpProjectNode projectNode, References references)
        {
            if (languageService == null)
                throw new ArgumentNullException("languageService");
            if (projectNode == null)
                throw new ArgumentNullException("projectNode");
            if (references == null)
                throw new ArgumentNullException("references");

            _languageService = languageService;
            _projectNode = projectNode;
            _references = references;
        }

        public void UpdateSchema()
        {
            if (DialogResult.Yes ==
                    MessageBox.Show(
                        SR.GetString(SR.WarningDataLoss),
                        SR.GetString(SR.Warning),
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button2))
            {
                try
                {
                    EntityRoot root = _languageService.GetCurrentRoot();
                    List<EntityTemplate> templates = new List<EntityTemplate>();
                    foreach (EntityTemplate t in root.Templates.Values)
                    {
                        ISchemaUpdater updater = TypeLoader.Load<ISchemaUpdater>(t.In, t.Type);
                        if (updater != null)
                        {
                            templates.Add(t);
                        }
                    }

                    ESharpSchemaUpdaterProgress progress = new ESharpSchemaUpdaterProgress(
                        _projectNode.Caption,
                        templates.ToArray());
                    progress.ShowDialog();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(
                        SR.GetString(ex.Message),
                        SR.GetString(SR.Error),
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// Indicates if this 
        /// </summary>
        /// <returns></returns>
        public bool CanUpdateSchema()
        {
            return true;
        }
    }
}
